<template>
  <div class="goods">
    <div class="goods-left">
      <van-sidebar v-model="sactive">
        <van-sidebar-item :title="item.name" v-for="(item,index) in goodsList" :key="index" @click="clickLeft(index)" />
      </van-sidebar>
    </div>
    <div class="goods-right">
      <div>
        <div class="good-container" v-for="(item,index) in store_goodsList" :key="index" :id="index">
          <div class="good-title">{{item.name}}</div>
          <ul>
            <li v-for="(value,i) in item.foods" :key="i" @click="jumpGoodsInfo(value)">
              <template>
                <img :src="value.imgUrl">
                <div>
                  <p>{{value.name}}</p>
                  <p><span>下饭</span></p>
                  <p><span>月售{{value.sellCount}}份</span>&nbsp;&nbsp;<span>好评度{{value.rating}}%</span> </p>
                  <p><span>￥{{value.price}}</span>&nbsp;&nbsp;<del>￥19.9</del></p>
                </div>
                <!-- vue阻止事件冒泡@事件类型.stop--由于给父元素绑定了点击事件,会使子元素也存在这个点击事件 -->
                <div class="addsubNum"><em @click.stop="addSubtract(value,-1)" v-show="value.num">-</em><span v-show="value.num">{{value.num}}</span><em @click.stop="addSubtract(value,1)">+</em></div>
              </template>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { getGoodsList_api } from "@/apis/goods.js";
import { getSeller_api } from "@/apis/seller.js";
import BScroll from "@better-scroll/core";

@Component({})
export default class extends Vue {
  sactive = 0;
  goodsList: any = []; //不知道怎么写typeScript
  storeInfo: any = {};
  goodsLeft: any;
  goodsRight: any;

  addSubtract(value, n) {
    //对象传地址
    value.num += n;
  }
  //计算属性，获得仓库中的商品列表
  get store_goodsList() {
    return this.$store.state.goodsList;
  }
  //左右联动效果
  async created() {
    //获取商品列表
    let res = await getGoodsList_api(); //异步
    this.goodsList = res.data.goodsList;

    //获取商家信息
    let result = await getSeller_api();
    this.storeInfo = result.data.data;

    //nextTick里的代码会在dom节点更新后执行
    //强制执行到下一个生命周期
    this.$nextTick(() => {
      this.goodsLeft = new BScroll(".goods-left", {
        probeType: 3, //BetterScroll 实时派发 scroll 事件，需要设置 probeType 为 3
        click: true, //允许点击事件
      }); //将实例对象挂载到this上
      this.goodsRight = new BScroll(".goods-right", {
        probeType: 3,
        click: true,
      }); //将实例对象挂载到this上
      //监听商品右边，如果发生滚动触发
      this.goodsRight.on("scroll", (position) => {
        //position.y获取距离(.goods-right)顶部的距离,要取绝对值Math.abs()
        let heightArr = this.comHeight;
        heightArr.forEach((item, index) => {
          if (
            Math.abs(position.y) >= item.start &&
            Math.abs(position.y) < item.end
          ) {
            this.sactive = index;
          }
        });
      });
    });
  }
  //函数，点击左边让右边滚动
  clickLeft(index: any) {
    //点击左边让右边滚动
    this.goodsRight.scrollToElement(document.getElementById(index), 1000);
  }
  //函数，点击跳转到商品详情页
  jumpGoodsInfo(value) {
    //动态传参-->params传参
    this.$router.push({
      name: "goodsInfo",
      params: value,
    });
  }
  //计算属性,得到节点高度
  get comHeight() {
    //计算属性，如果值没有发生变化，只执行一次
    //clientHeight-->元素高度不包括边框
    //offestHeight-->元素高度包括边框
    // console.log(document.getElementById(6)?.clientHeight);
    let heightArr: any[] = [];
    let startHeight = 0; //开启的高度
    let domHeight: any = 0; //每个元素的高度
    this.goodsList.forEach((item, index) => {
      domHeight = document.getElementById(index + "")?.clientHeight;
      heightArr.push({
        start: startHeight, //节点开始距离(.goods-right)的高度
        end: startHeight + domHeight, //节点结束距离(.goods-right)的高度
      });
      startHeight = startHeight + domHeight;
    });
    return heightArr;
  }
}
</script>

<style lang="scss" scoped>
.goods {
  display: flex;
  width: 100%;
  height: 100%;
}
.goods-left {
  overflow-y: scroll;
  .van-sidebar-item--select::before {
    background-color: #ffc300;
  }
}
.goods-right {
  flex: 1;
  overflow-y: scroll;
  background-color: #f4f4f4;
  .good-title {
    font-size: 14px;
    color: #555555;
    border-left: 4px solid #dddddd;
    line-height: 30px;
    padding-left: 4px;
  }
  ul {
    background-color: #fff;
    li {
      display: flex;
      align-items: center;
      padding: 10px 5px;
      position: relative;
      .addsubNum {
        position: absolute;
        top: 35%;
        right: 10%;
        display: flex;
        align-items: center;
        span {
          margin: 0 8px;
        }
        em {
          display: inline-block;
          width: 24px;
          height: 24px;
          font-weight: bold;
          font-size: 18px;
          border-radius: 50%;
          background-color: #ffc300;
          text-align: center;
          line-height: 24px;
        }
        em:first-of-type {
          color: #aaaaaa;
          border: 1px solid #dddddd;
          background-color: #fff;
          line-height: 20px;
        }
      }
      img {
        width: 80px;
        height: 80px;
        border-radius: 5px;
        margin-right: 5px;
      }

      p:first-child {
        font-weight: bold;
        font-size: 18px;
      }
      p:nth-child(2) {
        line-height: 30px;
        span {
          font-size: 12px;
          padding: 3px;
          background-color: #eeeeee;
          font-weight: bold;
        }
      }
      p:nth-child(3) {
        color: #999999;
        font-size: 12px;
        margin: 10px 0;
      }
      p:last-child {
        margin-top: 10px;
        span {
          color: #ff3300;
          font-size: 18px;
          font-weight: bold;
        }
        del {
          color: #999999;
          font-size: 14px;
        }
      }
    }
  }
}
</style>

